package com.lglin.service.user.Impl;

import com.lglin.bean.product.entity.Order;
import com.lglin.bean.user.entity.Address;
import com.lglin.bean.user.entity.User;
import com.lglin.common.bean.Review;
import com.lglin.common.constant.Constant;
import com.lglin.common.utils.other.QiniuCloudUtil;
import com.lglin.dao.user.UserDao;
import com.lglin.dao.user.UserRoleDao;
import com.lglin.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
@EnableTransactionManagement
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserRoleDao userRoleDao;

    @Override
    public boolean register(User user) {
        user.setImg(Constant.DEFAULT_IMG);
        int result = userDao.register(user);
        if (result > 0) {
            return userRoleDao.addUserRoleRelate(user) > 0;
        }
        return false;
    }

    @Override
    public boolean checkNickname(String nickname) {
        User user = userDao.checkNickname(nickname);
        return user == null;
    }

    @Override
    public boolean checkEmail(String email) {
        User user = userDao.checkEmail(email);
        return user == null;
    }

    @Override
    public User selectByEmail(String email) {
        return userDao.checkEmail(email);
    }

    @Override
    public User selectById(int id) {
        return userDao.selectById(id);
    }

    @Override
    public boolean updateUserInfo(int id,MultipartFile file, String nickname,String email) {
        String img=email+"-"+"pic";
        int result=userDao.updateUserInfo(id,img,nickname);
        if (result>0){
            User user=userDao.selectById(id);
            if (!user.getImg().equals(Constant.DEFAULT_IMG)){
                QiniuCloudUtil.deleteFile(img);
            }
            QiniuCloudUtil.uploadFile(file,img);
            return true;
        }
        return false;
    }

    @Override
    public boolean insertReview(Review review) {
        int result=userDao.insertReview(review);
        return result > 0;
    }

    @Override
    public List<Integer> selectUserAddressIdByUId(int id) {
        return userDao.selectUserAddressIdByUId(id);
    }

    @Override
    public List<Address> selectAddressesById(List<Integer> userAddressIds) {
        return userDao.selectAddressesById(userAddressIds);
    }

    @Override
    public boolean updateAddressById(Address address) {
        int result=userDao.updateAddressById(address);
        return result>0;
    }

    @Override
    public boolean insertAddress(Address address,int uid) {
        int result=userDao.insertAddress(address);
        if (result>0){
            return userRoleDao.addUserAddress(uid,address.getId()) > 0;
        }
        return false;
    }

    @Override
    public boolean deleteAddressById(int id) {
        int addressResult=userDao.deleteAddressById(id);
        int relateResult=userDao.deleteUserAddressRelate(id);
        if (addressResult>0&&relateResult>0){
            return true;
        }
        return false ;
    }

    @Override
    public boolean updatePasswordByEmail(String email, String encode_password) {
        int result=userDao.updatePasswordByEmail(email,encode_password);
        return result>0;
    }

    @Override
    public boolean payOrder(int uid, int aid, String message, Map<Integer, Integer> map) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        String sid = formatter.format(new Date()) + randomStr();
        Order order = new Order();
        order.setAid(aid);
        order.setMessage(message);
        order.setOsid(1);
        order.setUid(uid);
        order.setSid(sid);
        int result = userDao.insertOrder(order);
        System.out.println(result);
        if (result > 0) {
            int a = userDao.insertOrderProduct(order.getId(), map);
            System.out.println(a);
        }
        return false;
    }

    @Override
    public List<Map<String, Object>> userOrder(int uid) {
        return userDao.userOrder(uid);
    }

    @Override
    public List<Map<String, Object>> orderInfo(String sid) {
        return userDao.orderInfo(sid);
    }

    private static String randomStr() {
        String str = "";
        for (int i = 0; i < 5; i++) {
            str += (int) (Math.random()*(10-1)+1);
        }
        return str;
    }

    public static void main(String[] args) {

        System.out.println("2020043020223718843".length());


    }


}
